package www.sharetime.weshare.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.OnClick;
import www.sharetime.weshare.R;
import www.sharetime.weshare.fragment.NavFragment;
import www.sharetime.weshare.interfaces.OnTabReselectListenerInf;
import www.sharetime.weshare.utils.L;
import www.sharetime.weshare.utils.ToastUtils;
import www.sharetime.weshare.view.NavigationButton;

/**
 * File Description  : 主页面
 *
 * @author : zhanggeng
 * @version : v1.0
 * @email : zhanggengdyx@gmail.com
 * @date : 2017/9/12 19:55
 * **************修订历史*************
 */


public class MainActivity extends FragmentActivity implements NavFragment.OnNavigationReselectListener {

    public static final String ACTION_NOTICE = "ACTION_NOTICE";

//    @BindView(R.id.activity_main_ui)
    Fragment mMainUi;

    private NavFragment mNavBar;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager manager = getSupportFragmentManager();
        mMainUi = manager.findFragmentById(R.id.activity_main_ui);
        mNavBar = ((NavFragment) manager.findFragmentById(R.id.fag_nav));
        mNavBar.setup(this, manager, R.id.main_container, this);
    }
//
//    @Override
//    protected int getLayoutId() {
//        return R.layout.activity_main;
//    }

//    @Override
//    public void initView(Bundle savedInstanceState) {
//
//        FragmentManager manager = getSupportFragmentManager();
//        mNavBar = ((NavFragment) manager.findFragmentById(R.id.fag_nav));
//        mNavBar.setup(this, manager, R.id.main_container, this);
//    }



    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        doNewIntent(intent, false);
    }

    private void doNewIntent(Intent intent, boolean isCreate) {
        if (intent == null || intent.getAction() == null)
            return;
        String action = intent.getAction();
        Log.e("TAG", "onNewIntent action:" + action + " isCreate:" + isCreate);
        if (action.equals(ACTION_NOTICE)) {
            NavFragment bar = mNavBar;
            if (bar != null) {
                bar.select(3);
            }
        }
    }

    @Override
    public void onReselect(NavigationButton navigationButton) {
        Fragment fragment = navigationButton.getFragment();
        if (fragment != null
                && fragment instanceof OnTabReselectListenerInf) {
            OnTabReselectListenerInf listener = (OnTabReselectListenerInf) fragment;
            listener.onTabReselect();
        }

        Log.e(MainActivity.class.getSimpleName(), "onReselect");
    }



    @OnClick({R.id.main_container})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_container:
                break;
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            L.dt("back", "keycode");
            exitBy2Click(); // 调用双击退出函数
        }
        return false;
    }

    /**
     * 双击退出函数
     */
    private static Boolean isExit = false;

    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true; // 准备退出
            ToastUtils.showToastShort(this, "再按一次退出");
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

        } else {
            finish();
        }
    }





}
